/**********************************************************
 * AdminLoginAction.java
 *
 * Created by Nishan
 * Last modified Date: Aug 1, 2008 9:18:42 AM by nishan
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.Preparable;
import com.printeverywhere.delegator.AdminServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.Admin;
import com.printeverywhere.dto.AdminModules;
import com.printeverywhere.dto.Module;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class AdminRightsAction.
 */
public class AdminRightsAction extends AdminSystemSetup implements
		ServletRequestAware, Preparable {

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */

	public void prepare() throws Exception {
		SearchCriteria searchCriteria=new SearchCriteria();
		searchCriteria.setAsc(true);
		searchCriteria.setOrderBy("moduleName");
		adminModules = (List<Module>) CommonFunctionDelegator.GeneralgetCriteriaList("com.printeverywhere.dto.Module",searchCriteria);
		admins=AdminServiceDelegator.adminUserList();
	}

	/** The Constant logger. */
	private static final Logger logger = Logger.getLogger(AdminRightsAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The request. */
	private HttpServletRequest request;

	/** The admin. */
	private Admin admin;

	/** The admin modules. */
	private List<Module> adminModules;

	/** The allowed modules. */
	private List<Integer> allowedModules;

	/** The admins. */
	private List<Admin> admins;

	/**
	 * Gets the admins.
	 *
	 * @return the admins
	 */
	public List<Admin> getAdmins() {
		return admins;
	}

	/**
	 * Sets the admins.
	 *
	 * @param admins the new admins
	 */
	public void setAdmins(List<Admin> admins) {
		this.admins = admins;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		if(admin!=null && admin.getUsername()!=null){
			admin = AdminServiceDelegator.getAdminByName(admin.getUsername());
			Set<AdminModules> set = admin.getAdminModuleses();
			allowedModules=new ArrayList<Integer>();
			Iterator<AdminModules>it = set.iterator();
			while (it.hasNext()) {
				AdminModules module = (AdminModules) it.next();
				allowedModules.add(module.getModule().getId());
			}
		}
		return SUCCESS;
	}

	/**
	 * Save.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String save() throws Exception {
		if(admin==null){
			addActionError("Sorry your session is expired");
			return INPUT;
		}

		if(admin.getId()==null && AdminServiceDelegator.getAdminByName(admin.getUsername())!=null){
			addActionError("user with this username already exists");
			return INPUT;
		}

		if (allowedModules != null && allowedModules.size() > 0){
			Set<AdminModules> set=null;
			Iterator<AdminModules>it=null;
			if(admin.getId()==null )
			set=new HashSet<AdminModules>();
			else{
				set = AdminServiceDelegator.getAdminByName(admin.getUsername()).getAdminModuleses();
			}
			if(set.size()==0){
				for (Integer moduleid : allowedModules) {
					AdminModules ad =new AdminModules(new Module(moduleid));
					set.add(ad);
				}
			}
			else{
				it = set.iterator();
				for (Integer moduleid : allowedModules) {
					AdminModules ad;
					if(it.hasNext()){
						ad=it.next();
						ad.getModule().setId(moduleid);
					}else{
						ad=new AdminModules(new Module(moduleid));
					}
					set.add(ad);
				}
			}
			admin.setAdminModuleses(set);
			CommonFunctionDelegator.generalInsertbyobj(admin);
			while(it!=null && it.hasNext()){
				CommonFunctionDelegator.GeneralDelete(it.next());
			}
		}else{
			addActionError("Please select admin modules");
			return INPUT;
		}

		addActionMessage("User Successfully Saved");

		return SUCCESS;

	}

	/**
	 * Delete.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String delete() throws Exception {

		CommonFunctionDelegator.GeneralDelete(admin);
		addActionMessage("User Successfully Deleted");
		return SUCCESS;

	}

	/* (non-Javadoc)
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	/**
	 * Gets the admin.
	 *
	 * @return the admin
	 */
	public Admin getAdmin() {
		return admin;
	}

	/**
	 * Sets the admin.
	 *
	 * @param admin the new admin
	 */
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	/**
	 * Gets the admin modules.
	 *
	 * @return the admin modules
	 */
	public List<Module> getAdminModules() {
		return adminModules;
	}

	/**
	 * Sets the admin modules.
	 *
	 * @param adminModules the new admin modules
	 */
	public void setAdminModules(List<Module> adminModules) {
		this.adminModules = adminModules;
	}

	/**
	 * Gets the allowed modules.
	 *
	 * @return the allowed modules
	 */
	public List<Integer> getAllowedModules() {
		return allowedModules;
	}

	/**
	 * Sets the allowed modules.
	 *
	 * @param allowedModules the new allowed modules
	 */
	public void setAllowedModules(List<Integer> allowedModules) {
		this.allowedModules = allowedModules;
	}

}
